<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title><? echo $title."&nbsp;--&nbsp".$promotion_name ?></title>
    <? require("lib.php"); ?>
    <link rel="stylesheet" href="css/jquery.datetimepicker.css" type="text/css" media="screen" />
    <script type="text/javascript" src="js/jquery.datetimepicker.js"></script>
</head>
<body>
<div class="content-box"><!-- Start Content Box -->
    <div class="content-box-header">
        <h3><? echo $title."&nbsp;--&nbsp".$promotion_name ?></h3>
        <div class="clear"></div>
    </div> <!-- End .content-box-header -->
    <div class="content-box-content">
        <?
        switch ($do) {
            case "add":
                add();
                break;
            case "update":
                update($info);
                break;
            case "delete":
                delete($id);
                break;
            case "cate_add":
                add_cate();
                break;
            default:
                show_info($info);
        }
        ?>
        <? function show_box($row, $iseditmode, $isadd=0){
            global $id, $h, $url, $title, $sales_promotion, $baseurl, $option, $lang, $code_array, $promotion_id, $promotion_name, $cate_info;
            $full = isset($row['full']) ? $row['full'] : '';
            $promotion_id = isset($row['promotion_id']) ? $row['promotion_id'] : $promotion_id;
            $cut = isset($row['cut']) ? $row['cut'] : '';
            $arr_cate=$code_array['discount_type'];
            echo $h->hidden('promotion_id', $promotion_id);

            $tree=unlimitedlevel($cate_info);
            $cate_array=array();
            foreach($tree as $val){
                $cate_array[$val['id']]=array(
                    'id'=>$val['id'],
                    'name'=>$val['html_prefix'].$val['name_'.$lang],
                );
            }
            ?>
            <table width="99%" cellspacing="0" cellpadding="3" class="mbox">
                <tr class="th"><td class="thtitle" width="120" align="right"><? echo $title ?></td>
                    <td><? if(isset($row['id'])) echo $h->hidden('id', iso($row["id"])); ?></td></tr>
                <tr><td class="l"><?=ROOT_SALES_PROMOTION_NAME?></td><td><input class="g" name="promotion_name" value="<?echo $promotion_name?>" readonly/></td></tr>
                <tr class="hide"><td class="l">优惠方式</td><td><? echo $h->select('discount_type', 0, $arr_cate, 'id', 'name_'.$lang, 'g gs'); ?></td></tr>
                <tr class="hide"><td class="l"><?=ROOT_CUSTOMER_DISCOUNT?></td><td><? echo $h->text('discount_value', $full, 'g gs') ?></td></tr>
                <tr><td class="l"><?=ROOT_CATEGORY?></td><td><? echo $h->select('category_id', 0, $cate_array, 'id', 'name', 'g', 'select_change(this)') ?></td></tr>
                <tr><td class="l"><?=ROOT_PRODUCT?></td><td class="product_list" style="height: 300px">123</td></tr>
                <tr><td class="l">&nbsp;</td><td><br>&nbsp;&nbsp;<a onClick="history.go(-1)">[ <?=ROOT_BACK?> ]</a><br><br></td></tr>
            </table>
        <? } ?>
        <? function show_boxes($result){
            global $url, $page, $pages, $cateid, $s, $lang, $arrlang, $filter, $h, $code_array, $promotion_id, $promotion_name, $sales_promotion_list, $promotion_type;
            $operation = '';
            $list_operation = '';
            if($promotion_type==2){
                $operation = '<a href="'.$url.'.php?do=add&promotion_id='.$promotion_id.'">[ '.ROOT_SALES_PROMOTION_ADD_CATE.' ]</a>';
                $list_name = ROOT_SALES_PROMOTION_DISCOUNT_CATE;
                $list_operation = '<a href="#">'.ROOT_SALES_PROMOTION_DISCOUNT_CANCLE.'</a>';
            }else{
                $operation = '<a href="'.$url.'.php?do=add&promotion_id='.$promotion_id.'">['.ROOT_SALES_PROMOTION_ADD_PRODUCT.']</a>';
                $list_name = ROOT_SALES_PROMOTION_DISCOUNT_PRODUCT;
                $list_operation = '<a>'.ROOT_SALES_PROMOTION_DISCOUNT_CANCLE.'</a>';
            }
            $str='<table width="99%" cellspacing="0" class="mlist"><tr class="th"><td><strong>'.ROOT_SALES_PROMOTION_NAME.'</strong></td><td><strong>'.$list_name.'</strong></td><td><strong>'.ROOT_CUSTOMER_DISCOUNT.'</strong></td><td>'.$operation.'</td></tr>';
            $count=1;
            foreach( $result as $row)
            {
                $id=$row['id'];
                $name = $row['name_'.$lang];
                $discount_type = $row['discount_type'];
                $discount_value = $row['discount_value'];

                $list_operation = "<a href=\"$url.php?do=delete&promotion_id=".$promotion_id."&id=".$id."\">".ROOT_SALES_PROMOTION_DISCOUNT_CANCLE."</a>";
                $str.="<tr><td>".$promotion_name."</td><td>".$name."</td><td>".$discount_value."</td><td>&nbsp;</td></tr>";
                $count++;
            }
            $str.= "</table>";
            echo $str;
            if($count==1) echo "<br>No data now. (".ROOT_NO_DATA."!)";
            echo echo_page_raw($page, $pages, $url.'.php?promotion_id='.$promotion_id.'&page=');
        } ?>
        <?function show_cate($cate_info, $iseditmode, $isadd = 0){
            global $id, $h, $url, $title, $sales_promotion_list, $baseurl, $option, $lang, $code_array, $promotion_id, $promotion_name, $product_category;
            $str='<table width="99%" cellspacing="0" class="mlist"><tr class="th"><td><strong>&nbsp;</strong></td><td><strong>'.ROOT_SALES_PROMOTION_DISCOUNT_CATE.'</strong></td><td><strong>'.ROOT_SALES_PROMOTION_TYPE.'</strong></td><td><strong>'.ROOT_CUSTOMER_DISCOUNT.'</strong></td><td><strong>'.ROOT_SALES_PROMOTION_DISCOUNT_ADD.'<strong></td></tr>';
            $tree=array(); // tree = category tree 分类树
            $tree=unlimitedlevel($cate_info);
            //print_r($tree);
            $count=1;
            $arr_cate=$code_array['discount_type'];
            foreach( $tree as $row){
                $id=decode($row['id']);
                if($row['level']>3) continue; // level <= 3, 最大级别为3级
                if($row['level']==3) $will_add_sub='';
                if($row['discount_type'] != 0){
                    $type=$arr_cate[$row['discount_type']]['name_'.$lang];
                }else{
                    $type = '';
                }
                $discount_value = $row['discount_value'];
                $cate_promotion_id = $row['promotion_id'];
                $promotion_list = $sales_promotion_list->get_row($cate_promotion_id);
                if($promotion_id == $cate_promotion_id){
                    $str.="<tr><td>".$h->hidden('id', $id)."</td><td>".$row['html_prefix'].decode($row['name_'.$lang])." &nbsp; </td><td>".$type."</td><td>".$discount_value."</td><td>".ROOT_SALES_PROMOTION_DISCOUNT_ADD1." | <a href='javascript:void(0)' onclick=\"cate_cancel(this)\">".ROOT_SALES_PROMOTION_DISCOUNT_CANCLE."</a></td></tr>";
                }elseif($promotion_list['end_time'] >= date('Y-m-d H:i:s', time())){
                    $str.="<tr><td>&nbsp;</td><td>".$row['html_prefix'].decode($row['name_'.$lang])." &nbsp; </td><td>".$type."</td><td>".$discount_value."</td><td>".ROOT_SALES_PROMOTION_DISCOUNT_ADD2."</td></tr>";
                }else{
                    $str.="<tr><td>".$h->hidden('id', $id)."</td><td>".$row['html_prefix'].decode($row['name_'.$lang])." &nbsp; </td><td>".$h->select('type', 0, $arr_cate, 'id', 'name_'.$lang, 'g gs')."</td><td>".$h->text('discount_value', '', 'g gs')."</td><td><a onclick=\"cate_add(this)\" href=\"javascript:void(0)\">".ROOT_SALES_PROMOTION_DISCOUNT_ADD."</a></td></tr>";
                }
    			$count++;
    		}
            $str .= '<tr><td class="l">&nbsp;</td><td><br>&nbsp;&nbsp;<a onclick="history.go(-1)">[ '.ROOT_BACK.' ]</a><br><br></td></tr>';
    		$str.= "</table>";
    		echo $str;
        }?>
        <br class="clear" />
    </div> <!-- End .content-box-content -->
    <br class="clear" />
</div><!-- End .content-box -->
<script type="text/javascript">
<?
if($promotion_type==1){
    $add_type_url = "discount_product_add.php?do=product_add";
    $cancel_type_url = "discount_product_add.php?do=product_cancel";
}
else{
    $add_type_url = "discount_add.php?do=cate_add";
    $cancel_type_url = "discount_add.php?do=cate_cancel";
}
?>
function cate_add(node){
    var parent_td = $(node).parent().parent().children('td');

    $.ajax({
        url: '<?=$add_type_url?>',
        data: {'id': parent_td.eq(0).children('input:hidden').val(),
                'discount_type': parent_td.eq(2).children('select').val(),
                'discount_type_name':parent_td.eq(2).children('select').find('option:selected').text(),
                'discount_value':parent_td.eq(3).children('input').val(),
                'promotion_id':<?=$promotion_id?>},
        type: 'post',
        success:function(data1){
            var data_arr = eval("("+data1+")");
            parent_td.eq(2).html(data_arr.discount_type_name);
            parent_td.eq(3).html(data_arr.discount_value);
            parent_td.eq(4).html("<?=ROOT_SALES_PROMOTION_DISCOUNT_ADD1?> | <a href='javascript:void(0)' onclick='cate_cancel(this)'><?=ROOT_SALES_PROMOTION_DISCOUNT_CANCLE?></a>");
        }
    });
}

function cate_cancel(node){
    var parent_td = $(node).parent().parent().children('td');
    $.ajax({
        url: '<?=$cancel_type_url?>',
        data: {'id':parent_td.eq(0).children('input:hidden').val()},
        type: 'get',
        success:function(datas){
            //parent_td.eq(2).html("<select name=\"type\" class=\"g\"><option value=\"1\">百分比</option><option value=\"2\">实际</option></select>");
            parent_td.eq(2).html('<? echo  $h->select("discount_type", 0, $code_array["discount_type"], "id", "name_".$lang, "g gs");?>');
            parent_td.eq(3).html("<input type='text' value='' name='discount_value' class='g gs'/>");
            parent_td.eq(4).html("<a href='javascript:void(0)' onclick='cate_add(this)'><?=ROOT_SALES_PROMOTION_DISCOUNT_ADD?></a>");
        }
    });
}

function select_change(node){
    var node_val = $(node).val();
    var discount_type = $('select[name="discount_type"]').val();
    var discount_type_name = $('select[name="discount_type"]').find('option:selected').text();
    var discount_value = $('input[name="discount_value"]').val();
    $.ajax({
        url: "discount_product_add.php",
        data: {'cate_id': node_val,
               'discount_type': discount_type,
               'discount_type_name': discount_type_name,
               'discount_value': discount_value,
               'promotion_id': <?=$promotion_id?>
              },
        type: 'get',
        success:function(data){
            $('.product_list').html(data);
        }
    });
}

$(function(){
    var select_change = $('select[name="category_id"]');
    select_change.triggerHandler('change');
});
</script>
</body>
</html>
